// Windows/Registry.cpp

#include "StdAfx.h"



#include "Registry.h"

LONG CKey::Create(HKEY parentKey, LPCTSTR keyName,
    LPTSTR keyClass, DWORD options, REGSAM accessMask,
    LPSECURITY_ATTRIBUTES securityAttributes, LPDWORD disposition)
{
  DWORD dispositionReal;
  HKEY key = NULL;
  LONG res = RegCreateKeyEx(parentKey, keyName, 0, keyClass,
      options, accessMask, securityAttributes, &key, &dispositionReal);
  if (disposition != NULL)
    *disposition = dispositionReal;
  if (res == ERROR_SUCCESS)
  {
    res = Close();
    _object = key;
  }
  return res;
}
LONG CKey::Open(HKEY parentKey, LPCTSTR keyName, REGSAM accessMask)
{
  HKEY key = NULL;
  LONG res = RegOpenKeyEx(parentKey, keyName, 0, accessMask, &key);
  if (res == ERROR_SUCCESS)
  {
    res = Close();
    _object = key;
  }
  return res;
}

LONG CKey::Close()//
{
  LONG res = ERROR_SUCCESS;
  if (_object != NULL)
  {
    res = RegCloseKey(_object);
    _object = NULL;
  }
  return res;
}

LONG CKey::DeleteSubKey(LPCTSTR subKeyName)//
{
  return RegDeleteKey(_object, subKeyName);
}

LONG CKey::RecurseDeleteKey(LPCTSTR subKeyName)///////
{
  CKey key;
  LONG res = key.Open(_object, subKeyName, KEY_READ | KEY_WRITE);
  if (res != ERROR_SUCCESS)
    return res;
  FILETIME fileTime;
  const unsigned int kBufferSize = MAX_PATH + 1; // 256 in ATL
  DWORD size = kBufferSize;
  TCHAR buffer[kBufferSize];
  while (RegEnumKeyEx(key._object, 0, buffer, &size, NULL, NULL, NULL, &fileTime) == ERROR_SUCCESS)
  {
    res = key.RecurseDeleteKey(buffer);
    if (res != ERROR_SUCCESS)
      return res;
    size = kBufferSize;
  }
  key.Close();
  return DeleteSubKey(subKeyName);
}

LONG CKey::SetValue(LPCTSTR name, LPCTSTR value)///
{
  return RegSetValueEx(_object, name, NULL, REG_SZ,
      (const BYTE * )value, (lstrlen(value) + 1) * sizeof(TCHAR));
}

LONG CKey::QueryValue(LPCTSTR name, LPTSTR value, unsigned int &count)
{

  DWORD type = NULL;
  LONG res = RegQueryValueEx(_object, (LPTSTR)name, NULL, &type, (LPBYTE)value, (DWORD *)&count);
  return res;
}

BOOL CKey::GetUnInstallName( std::wstring &vecInstallName )
{
	std::wstring wsKeyName = L"SoftWare\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
	wsKeyName += vecInstallName;
	LONG res = Open(HKEY_LOCAL_MACHINE, wsKeyName.c_str(), KEY_READ );
	if (res != ERROR_SUCCESS)
		return FALSE;

// 	WCHAR buffer[256] = {0};
// 	DWORD dwIndex = 0;
// 	DWORD cbName = 256*sizeof(TCHAR);
// 	long lResult;
// 
// 	lResult = RegEnumKeyEx( _object , dwIndex, buffer, &cbName, 0, NULL, NULL, NULL); 
// 
// 	while ( lResult == ERROR_SUCCESS && lResult != ERROR_MORE_DATA )
// 	{
// 		if ( vecInstallName == buffer )
// 		{
// 			Close();
// 			return TRUE;
// 		}
// 
// 		dwIndex ++;
// 		cbName = 256*sizeof(WCHAR);  
// 		lResult = RegEnumKeyEx( _object, dwIndex, buffer, &cbName, 0, NULL, NULL, NULL); 
// 
// 	}
	Close();

	return TRUE;
}




